package com.ht.modules.authentication.utils;

import java.security.MessageDigest;
import java.util.Map;

public class Md5Util {

    public static String byteArrayToHexString(byte[] b) {
        StringBuilder resultSb = new StringBuilder();
        for (byte value : b) {
            resultSb.append(byteToHexString(value));
        }
        return resultSb.toString();
    }

    private static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigits[d1] + hexDigits[d2];
    }

    public static String MD5Encode(String origin, String charSetName) {
        String resultString;
        try {
            resultString = origin;
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (charSetName == null || charSetName.isEmpty()) {
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            } else {
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charSetName)));
            }
        } catch (Exception exception) {
            throw new RuntimeException(exception);
        }
        return resultString;
    }

    private static final String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    public static void main(String[] args) throws Exception {
        String encrypt = AesUtil.encrypt("dt_1", "5nKUAOpl0GPhEKG5");
        System.out.println("encrypt = " + encrypt);
        String md5Encode = MD5Encode("HARG/fZiXzyBf4AeY8huoA==", "UTF-8");
        System.out.println(md5Encode);
        String publicAesKey = AesUtil.getKey();
        System.out.println("publicAesKey = " + publicAesKey);
        String privateAesKey = AesUtil.getKey();
        System.out.println("privateAesKey = " + privateAesKey);
        String pubKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApaT9zZQWw7u84qilqm1PqemkxvB0W2bWMGsj2AdI+IvUWdQfPePXZjJ6ONbw6QW2Yg6VbcDvKRvi+893fYhj1koZSQfoZUcSVxvkO3yiqgNf+qolLsT944d3saKAWQ51jmB2PqEW1u6xwl+jXX9A5AjYY1qe6HrKaeXi7vbscpeb6taDPcQWFP25JTJf3oYgCmfhLITbHU8jOL+iZAH+FjTbiU3OSEs4+sFTekAr+DeFMFe75ZCjcWrAxPQ533si3QA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GcknOeMs5WBLh4nFny0SBaB4wlcN55qgFrNlBuFftxjHyaQs6cWsPEqwNU3OGyaIUu9bf7vHC4dQiSJ8+D0tZmohUOlo0Ue+rQxd7MlXhoAjsqQM+4AUfAo2FppYggVf58ri/JKiwHdBMqu8cLU45j6fQKBgQCWofgQYcRtAsj+2dgul9H1MyYvuQv5QZr7bHRFJiyOYuTV43npxblkOa2piDeS0eU+u8Pp+CcQSryBesD+IfV5ILfY1nlBeA/boGa0UZo/OApG124iY+4f97GB+Mx3BQ6XlloRz9x/GgZA0PbnbBWjit+khGXL6zjgEp3tFb09gQKBgGD6GNLg37HZpa1fg1qVLCBZhdosC/I6RAmKfXl0bjjIxJwh7lR8DAzaIaPRZm2aNp+J3gq/JFlkZBd/FneCUcYd++Y1K0y3WRJCMOEogAEILOEKHLc85WNozNm6yYYbEtOHdiUkoKp0xkmalJurAT1Vsoj33N6SvgDoECqQO8dxAoGAbh/BYyhlJCIbgBJV/jIl54ucGjb9d3kOzvVdje9ibs6WkcOAnhm44+yorc7PWHgCalLKCdFgLXFRW3BxB7xZyfwFHaDkB9PucvQ3ZzGFh/N3k7kbOl99/X3kPgIyOMlo1Cx4lswK/aLB6SozVq/GKL6UsPc/l6y30MhKdVri/CU=";
        System.out.println("priKey = " + priKey);
        String content = "9d71cbdaed90874db5301817e855f4f4";
        System.out.println("加密内容： " + content);
        // 私钥加密
        String string1 = RsaUtil.decryptByPrivateKey(content, priKey, RsaUtil.Mode.PRIVATE_ENCRYPT);
        System.out.println("私钥加密 = " + string1);
        // 公钥解密
        String string2 = RsaUtil.decryptByPrivateKey(string1, pubKey, RsaUtil.Mode.PUBLIC_DECRYPT);
        System.out.println("公钥解密 = " + string2);
        // 公钥加密
        String string3 = RsaUtil.decryptByPrivateKey(content, pubKey, RsaUtil.Mode.PUBLIC_ENCRYPT);
        System.out.println("公钥加密 = " + string3);
        // 私钥解密
        String string4 = RsaUtil.decryptByPrivateKey(string3, priKey, RsaUtil.Mode.PRIVATE_DECRYPT);
        System.out.println("私钥解密 = " + string4);

    }
}
